package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.CourseSchedulingAlways;
import com.xmy.cultivate.entity.response.LessonNumRs;
import com.xmy.cultivate.mapper.CourseSchedulingAlwaysMapper;
import com.xmy.cultivate.service.ICourseSchedulingAlwaysService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 日期总排课表，计算缺课使用 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2023-07-28
 */
@Service
public class CourseSchedulingAlwaysServiceImpl extends ServiceImpl<CourseSchedulingAlwaysMapper, CourseSchedulingAlways> implements ICourseSchedulingAlwaysService {

    @Autowired
    CourseSchedulingAlwaysMapper courseSchedulingAlwaysMapper;

    @Override
    public Result getLessonNumList(Integer yearPart, Integer quarterNum) {
        QueryWrapper<CourseSchedulingAlways> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year_part", yearPart);
        queryWrapper.eq("quarter_num", quarterNum);
        queryWrapper.eq("grade_section", 1);
        queryWrapper.orderByAsc("num");
        List<CourseSchedulingAlways> list = courseSchedulingAlwaysMapper.selectList(queryWrapper);
        CourseSchedulingAlways courseSchedulingAlwaysNow = courseSchedulingAlwaysMapper.getLessonNumNow(yearPart,quarterNum, LocalDate.now().toString());
        List<LessonNumRs> lessonNumList = new ArrayList<>();
        Integer lessonNumNow = 0;
        for (CourseSchedulingAlways courseSchedulingAlways : list) {
            LessonNumRs  lessonNumRs = new LessonNumRs();
            lessonNumRs.setId(courseSchedulingAlways.getId());
            lessonNumRs.setLessonNum(courseSchedulingAlways.getNum());
            lessonNumRs.setIsNow(0);
            if (courseSchedulingAlwaysNow!=null){
                if (courseSchedulingAlways.getNum().equals(courseSchedulingAlwaysNow.getNum())){
                    lessonNumRs.setIsNow(1);
                    lessonNumNow = courseSchedulingAlwaysNow.getNum();
                }
            }
            lessonNumList.add(lessonNumRs);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("lessonNumList", lessonNumList);
        result.put("lessonNumNow", lessonNumNow);
        Result rs = new Result(Code.OK,result,"");
        return rs;
    }

}
